/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 2013-2014 sagyf Yang. The Four Group.
 */

define(['adminapp', 'bootstrap-datepicker', 'bootstrap-datepicker-zh', 'custom/components-pickers'], function ($app) {
    $app.navactive('member');
    ComponentsPickers.init();

    var $member_form = $('#member_form');

    $member_form.validate({
        errorElement: 'span', //default input error message container
        errorClass  : 'help-block', // default input error message class
        focusInvalid: false, // do not focus the last invalid input
        ignore      : "",
        rules       : {
            'member.name'        : {
                minlength: 2,
                required : true,
                username : true
            },
            'member.card_no'        : {
                minlength: 6,
                required : true,
                // ajax 验证
                remote : {
                    url: g.ctx + 'customer/member/checkno',
                    data:{
                        'member.id': function(){
                            return $('#id').val();
                        }
                    }
                }
            },
            'member.nationality' : {
                required: true
            },
            'member.gender'      : {
                required: true
            },
            'member.id_card'     : {
                required: true,
                idcard  : true
            },
            'member.phone'       : {
                required  : true,
                'zh-phone': true
            },
            'member.postcode'    : {
                required     : true,
                'zh-postcode': true
            },
            'member.mail_address': {
                required: true
            },
            'member.email'       : {
                email: true
            }
        },
        messages: {
            'member.card_no': {
                remote: '该会员卡号已经被使用，请还其他的会员卡号'
            }
        },
        invalidHandler: function (event, validator) {

        },

        errorPlacement: function (error, element) { // render error placement for each input type
            var icon = $(element).parent('.input-icon').children('i');
            icon.removeClass('fa-check').addClass("fa-warning");
            icon.attr("data-original-title", error.text()).tooltip({'container': 'body'});
        },

        highlight: function (element) { // hightlight error inputs
            var $group = $(element).closest('.form-group');
            $group.removeClass('has-success').addClass('has-error'); // set error class to the control group
        },

        unhighlight: function (element) { // revert the change done by hightlight

        },

        success: function (label, element) {
            var icon = $(element).parent('.input-icon').children('i');
            $(element).closest('.form-group').removeClass('has-error').addClass('has-success'); // set success class to the control group
            icon.removeClass("fa-warning").addClass("fa-check");
        }
    });
});